package com.lanou.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.bean.AdminInfo;
import com.lanou.mapper.AdminInfoMapper;
import com.lanou.mapper.RoleMapper;
import com.lanou.service.AdminInfoService;
import com.lanou.service.RoleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

import static org.apache.shiro.web.filter.mgt.DefaultFilter.roles;

/**
 * Created by dllo on 18/3/9.
 */
@Service
public class AdminInfoServiceImpl implements AdminInfoService {
    @Resource
    private AdminInfoMapper adminInfoMapper;

    @Resource
    private RoleMapper roleMapper;

    @Override
    public AdminInfo login(@Param("username") String username, @Param("password") String password) {
        return adminInfoMapper.selectlogin(username, password);
    }

    @Override
    public List<AdminInfo> findAdminById(String username) {
        return adminInfoMapper.selectRole(username);
    }

    @Override
    public List<AdminInfo> findAllAdmin() {
        return adminInfoMapper.selectAll();
    }

    @Override
    public PageInfo<AdminInfo> queryAdminByPage(Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 2 : pageSize;
        PageHelper.startPage(pageNo, pageSize);
        List<AdminInfo> adminList = adminInfoMapper.selectAll();
        PageInfo<AdminInfo> adminPageInfo = new PageInfo<AdminInfo>(adminList);
        return adminPageInfo;
    }

    @Override
    public void addAdmin(String adminCode, String password, String name, String telephone, String email, List<Integer> roles) {
        Date enrolldate = new Date(System.currentTimeMillis());
        AdminInfo adminInfo = new AdminInfo();
        adminInfo.setAdminCode(adminCode);
        adminInfo.setPassword(password);
        adminInfo.setName(name);
        adminInfo.setTelephone(telephone);
        adminInfo.setEmail(email);
        adminInfo.setEnrolldate(enrolldate);
        adminInfoMapper.addAdmin(adminInfo);
        int adminId = adminInfo.getAdminId();
        for (Integer role : roles) {
            adminInfoMapper.addRoles(adminId, role);
        }
    }

    @Override
    public void updateAdmin(String admin, String name, String telephone, String email, Integer adminId,List<Integer> roles) {
        adminInfoMapper.updaAdmin(admin, name, telephone, email, adminId);
        roleMapper.deleteByAdmin(adminId);
        for (Integer role : roles) {
            adminInfoMapper.addRoles(adminId,role);
        }
    }
    @Override
    public void deleteAdmin(Integer adminId) {
        adminInfoMapper.delete(adminId);
    }


    @Override
    public List<AdminInfo> selectAdminRoleByName(String username) {
        return adminInfoMapper.selectAdminRoleByName(username);
    }

    @Override
    public void updateUserInfo(@Param("name") String name, @Param("telephone") String telephone, @Param("email") String email, @Param("adminCode") String adminCode) {
        adminInfoMapper.updateUserInfo(name,telephone,email,adminCode);
    }

    @Override
    public List<AdminInfo> selectOldPasswordExist(@Param("password") String password, @Param("adminCode") String adminCode) {
        return adminInfoMapper.selectOldPasswordExist(password,adminCode);
    }

    @Override
    public void updatePassword(@Param("password") String password, @Param("adminCode") String adminCode) {
        adminInfoMapper.updatePassword(password,adminCode);
    }

    @Override
    public void resetpass(List<Integer> admins) {
        for (Integer integer : admins) {
            adminInfoMapper.resetpassById(integer);
        }
    }
}
